<?php
/**
 * SemesterClass Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * semester_class entry
 *
 * @version $Id: SemesterClass.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Semester_Model_SemesterClassMapper
 * @package semester
 * @subpackage Model
 */
class Semester_Model_SemesterClass extends Semester_Model_Doctrine_BaseSemesterClass
{

    protected $_arrayData = array();


    protected function _getArrayData()
    {
        if (!$this->_arrayData) {
            $locale = Bootstrap::$config['resources']['locale']['default'];
            $path = dirname(__FILE__)."/../languages/$locale/data.ini";
            $ini = new Zend_Config_Ini($path);
            $this->_arrayData = $ini->get('array_data')->toArray();
        }
    }



    /**
     * 取得目前設定的班級數
     *
     * @param integer $year
     * @param integer $semester
     * @return array
     */

    function getClassSetNum($year,$semester)
    {
        $q = Doctrine_Query::create()
        ->select ('grade,COUNT(*) AS cc')
        ->from('Semester_Model_SemesterClass')
        ->where ('year=? AND semester=? AND enable=?',array(
        (int)$year, (int)$semester, 'Y'))
        ->groupBy('year, semester, grade');
        $row = $q->fetchArray();

        $arr = array();
        foreach($row as $val){
            $arr[$val['grade']] = $val['cc'];
        }
        return $arr;
    }

    /**
     * 班級名稱類別
     *
     * @return array
     */
    function getClassNameKind()
    {
        $this-> _getArrayData();
        return $this->_arrayData['className'];
        //return array("","一、二、三","甲、乙、丙","忠、孝、仁","其他");
    }

    /**
     * 班級名稱陣列
     *
     * @return array
     */
    function getClassCName()
    {
        $this-> _getArrayData();
        $arr = array();
        foreach($this->_arrayData['classCname'] as $id=>$val)
        $arr[$id] = explode(",",$val);
        return $arr;
        /*
         return array(
         0=>'',
         1=>array("","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一","三十二","三十三","三十四","三十五","三十六","三十七","三十八","三十九","四十","四十一","四十二","四十三","四十四","四十五","四十六","四十七","四十八","四十九","五十"),
         2=>array("","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"),
         3=>array("","忠","孝","仁","愛","信","義","和","平"),
         4=>''
         );
         */
    }


    /**
     * 取出班級名稱設定的類型
     *
     * @param integer $year
     * @param integer $semester
     * @return array
     */
    function getClassSetName($year,$semester)
    {
        $q = Doctrine_Query::create()
        ->select('grade, chinese_name')
        ->from('Semester_Model_SemesterClass')
        ->where ('year=? AND semester=? AND enable=?',array(
        (int)$year, (int)$semester, 'Y'))
        ->groupBy('year, semester, grade');
        $row = $q->fetchArray();

        $arr = array();
        $class_name_arr = $this->getClassCName();
        foreach($row as $val){
            $grade = $val['grade'];
            $chinese_name = $val['chinese_name'];
            if(in_array($chinese_name,$class_name_arr[1]))
            $arr[$grade]=1;
            elseif(in_array($chinese_name,$class_name_arr[2]))
            $arr[$grade]=2;
            elseif(in_array($chinese_name,$class_name_arr[3]))
            $arr[$grade]= 3;
            elseif(!empty($chinese_name))
            $arr[$grade]= 4;
        }

        return $arr;

    }


    public function getTutors($year=null,$semester=null)
    {
        if ($year==null) {
            $result = $cache->load('semester');
            $year = $result['year'];
            $semester = $result['semester'];
        }elseif ($semester == null) {
            $temp = explode("-",$year);
            $year = $temp[0];
            $semester = $temp[1];
        }
        $q = Doctrine_Query::create()
        ->from('Semester_Model_SemesterClass a')
        ->where('year=? AND semester=?', array($year, $semester))
        ->orderby('grade, class_sn');
        $rows = $q->fetchArray();
        $arr = array();
        foreach ($rows as $row) {
            $class_sn = sprintf("%d%02d",$row['grade'],$row['class_sn']);
            $arr[$class_sn]['tutor_name_1'] = $row['tutor_name_1'];
            $arr[$class_sn]['tutor_name_2'] = $row['tutor_name_2'];
        }
        return $arr;
    }

    /**
     * 班級陣列
     * @param $year
     * @param $semester
     * @return unknown_type
     */
    function getClassNames($year=null,$semester=null,$grade=null)
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/class.php";
        $tr = Zend_Registry::get('Zend_Translate');

        $cache = Zend_Registry::get('cache');
        if ($year==null) {
            $result = $cache->load('semester');
            $year = (int)$result['year'];
            $semester = (int)$result['semester'];
        }elseif ($semester == null) {
            $temp = explode("-",$year);
            $year = (int)$temp[0];
            $semester = (int)$temp[1];
        }
        $result = $cache->load('school');
        $grade_name = unserialize($result['length_name']);
        $q = Doctrine_Query::create()
        ->from('Semester_Model_SemesterClass')
        ->where('year=? AND semester=? AND enable=?',array($year, $semester, 'Y'));
        if ($grade)
        $q->andWhere('grade=?', $grade);
        $q->orderby('grade, class_sn');
        $res = $q->fetchArray();

        $arr = array();
        foreach($res as $row){
            if ($grade) {
                $id = $row['class_sn'];
                $arr[$id] = $row['chinese_name'].$tr->translate('class unit');
            }
            else {
                $id = $row['grade'].'-'.$row['class_sn'];
                $arr[$id] = $grade_name[$row['grade']].$row['chinese_name'].$tr->translate('class unit');
            }

        }
        return $arr;

    }

    /**
     * 班級年級陣列
     * @param $year
     * @param $semester
     * @return 二維 array
     */

    function getGradeClassNames($year=null,$semester=null,$grade=null)
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/class.php";
        $tr = Zend_Registry::get('Zend_Translate');
        $tr->addTranslation($path);

        $cache = Zend_Registry::get('cache');
        if ($year==null) {
            $result = $cache->load('semester');
            $year = $result['year'];
            $semester = $result['semester'];
        }elseif ($semester == null) {
            $temp = explode("-",$year);
            $year = $temp[0];
            $semester = $temp[1];
        }
        $result = $cache->load('school');
        $grade_name = unserialize($result['length_name']);

        $q = Doctrine_Query::create()
        ->select('a.grade, a.class_sn, a.chinese_name')
        ->from('Semester_Model_SemesterClass a')
        ->where('a.year=? AND a.semester=? AND a.enable=?',array($year,$semester,'Y'));
        if ($grade)
        $q->andWhere('a.grade=?', $grade);

        $q->orderBy('a.grade,  a.class_sn');
        $res = $q->fetchArray();

        $arr = array();
        foreach($res as $row){
            $id = $row['grade'].'-'.$row['class_sn'];
            if (isset($grade_name[$row['grade']]))
            $arr[$grade_name[$row['grade']].$tr->translate('grade unit')][$id] =
            $grade_name[$row['grade']].$row['chinese_name'].$tr->translate('class unit');
        }

        return $arr;

    }

}
